package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.app.dto.CommentDeleteDto;
import com.zhentao.model.common.app.dto.CommentFindAllDto;
import com.zhentao.model.common.app.vo.CommentAndUserVO;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.app.pojo.Comment;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApCommentService;
import com.zhentao.mapper.ApCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 薄超轩
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2024-10-09 16:49:21
*/
@Service
public class ApCommentServiceImpl extends ServiceImpl<ApCommentMapper, Comment>
    implements ApCommentService {
@Autowired
private ApCommentMapper apCommentMapper;
    @Override
    public ResponseResult commentFindAll(CommentFindAllDto dto) {
    List<CommentAndUserVO>comments=apCommentMapper.commentFindAll(dto);
        return ResponseResult.okResult(comments);
    }



    /**
     * app端：添加评论表
     * @param comment
     * @return
     */
    @Override
    public ResponseResult saveCommnet(Comment comment) {
        if (comment!=null){
            apCommentMapper.insert(comment);
            return ResponseResult.okResult("评论成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


    /**
     * app端：删除评论操作
     * @param dto
     * @return
     */
    @Override
    public ResponseResult deleteComment(CommentDeleteDto dto) {
        if (dto!=null){
            Comment comment = apCommentMapper.selectById(dto.getId());
            if (comment.getUserId().equals(dto.getUserId())){
                apCommentMapper.deleteById(dto.getId());
                return ResponseResult.okResult("删除成功");
            }else {
                return ResponseResult.errorResult(808, "只能删除自己的评论");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
}




